import axios from 'axios'
import {getLocal, getSession, getToken, setToken} from "./auth";
import { accessTokenRefresh } from "@/assets/api";
import router from '../../router'
import {  message } from 'ant-design-vue';
const service = axios.create({
    baseURL: '', // api的base_url
    withCredentials: true,
    timeout: 100000 // 请求超时时间
})
service.interceptors.request.use(config => {
    config.headers['token'] = config.data?.token || getToken()
    config.headers['refreshToken'] = config.data?.refreshToken
    config.data = config.data || {}
    return config
}, error => {
    console.log(error)
    Promise.reject(error)
})


// respone拦截器
service.interceptors.response.use(
    result => {
        const res = result.data
        console.log(res)
        if(res.resultCode != 200){
            if(res.resultCode == 301 || res.code == 301 || res.resultCode == 302 || res.code == 302){
                message.error(res.resultMessage || res.message || res.msg)
                router.replace('/login')
                return Promise.reject(res.resultCode)
            }
            message.error(res.resultMessage || res.message || res.msg)
            return Promise.reject(res.resultCode)
        }
        return res
    },
    error => {
        message.error('接口错误请联系管理员')
        return Promise.reject(error)
    }
)
export default service
